import { Injectable, OnModuleInit, OnModuleDestroy } from '@nestjs/common';
import { DataSource, EntityManager } from 'typeorm';

@Injectable()
export class DatabaseService implements OnModuleInit, OnModuleDestroy {
  constructor(private readonly dataSource: DataSource) {}

  async onModuleInit() {
    // 连接已由TypeOrmModule初始化，这里可以执行额外的初始化操作
    console.log('数据库连接已建立');
  }

  async onModuleDestroy() {
    await this.dataSource.destroy();
    console.log('数据库连接已关闭');
  }

  getEntityManager(): EntityManager {
    return this.dataSource.manager;
  }
}